package cn.tiga.controller;


import cn.tiga.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "fallbackMethod")
public class OrderController {



    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getById/{id}")
    @HystrixCommand
    public Object getById(@PathVariable("id")Long id){
        //id改为应用名称
        //String url = "http://localhost:10030/user/getById/" + id;
        String url = "http://user-server/user/getById/" + id;
        return restTemplate.getForObject(url, User.class);
    }
    // 统一降级方法
    public Object fallbackMethod(){
        return new User(-1l, "服务熔断触发", "用户服务不可用！");
    }

}
